package top.wenzz.commons.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 钟廷员 on 2016/9/13.
 */
public class DateUtils {
    public static final String format_time = "yyyy-MM-dd HH:mm:ss";
    public static final String format_date = "yyyy-MM-dd";
    public static final String format_month = "yyyy-MM";
    public static final String format_year = "yyyy";

    /**
     * 获得时间戳
     * @param calendarField Calendar的field
     * @param time 和当前时间相差毫秒/秒/分/时/天等
     * @return 时间戳
     */
    public static Long getTimeInMillis(int calendarField, int time) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendarField, calendar.get(calendarField) + time);
        return calendar.getTimeInMillis();
    }

    public static Date formatDate(String time, String format) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(false);
        return dateFormat.parse(time);
    }

    public static String formatDateToString(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);// 定义格式
        return df.format(date).toString();
    }

    public static void main(String[] args) {
        System.out.println(new Timestamp(DateUtils.getTimeInMillis(Calendar.DAY_OF_YEAR, 3)));
    }
}
